package com.yanqu.road.server.manger.activity.chat;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.entity.servercenter.ChannelInfo;
import com.yanqu.road.entity.web.activity.GetActivityIdByServerIdReqParam;
import com.yanqu.road.logic.config.ChannelConfig;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.manger.ServerListMgr;
import com.yanqu.road.utils.http.HttpHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.util.*;

public class ActivityChatMgr extends TempMgr {

    private static Map<Integer, List<Integer>> serverList2Map = new HashMap<>();

    public static List<Integer> getActivityServerList2ByType(int type){
        List<Integer> list = serverList2Map.get(type);
        if(list == null){
            return Collections.emptyList();
        }
        return new ArrayList<>(list);
    }

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
//        activityReload();
        return true;
    }

    public static void activityReload() {
        long serverId = GameServer.getInstance().getServerId();
        int channelId = ServerListMgr.getChannelId(serverId);
        ChannelInfo channelInfo = ServerListMgr.getChannelInfo(channelId);
        if(channelInfo != null && !StringUtils.isNullOrEmpty(channelInfo.getGetActivityUrl())){
            GetActivityIdByServerIdReqParam getActivityIdByServerIdReqParam  = new GetActivityIdByServerIdReqParam();
            getActivityIdByServerIdReqParam.setType(3);
            getActivityIdByServerIdReqParam.setServerId((int)serverId);
            getActivityIdByServerIdReqParam.setServerTime(System.currentTimeMillis()/1000);

            Map<String, String> resultMap = HttpHelper.httpPostJson(channelInfo.getGetActivityUrl(), JSONObject.toJSONString(getActivityIdByServerIdReqParam), "UTF-8", new HashMap<>(),
                    ChannelConfig.CN_FORBIDDEN_WORD_CHECK_SOCKET_TIME_OUT,
                    ChannelConfig.CN_FORBIDDEN_WORD_CHECK_CONNECT_TIME_OUT,
                    ChannelConfig.CN_FORBIDDEN_WORD_CHECK_CONNECT_REQUEST_TIME_OUT);    //1秒超时时间
            if (resultMap != null && "200".equals(resultMap.get("code"))) {
                String resultStr = resultMap.get("body");
                if(!StringUtils.isNullOrEmpty(resultStr)) {
                    try {
                        Map<Integer, List<Integer>> map = JSONObject.parseObject(resultStr, new TypeReference<Map<Integer, List<Integer>>>() {
                        });
                        if (map != null && !map.isEmpty()) {
                            serverList2Map = map;
                        }
                    }catch (Exception e){
                        getLogger().error("", e);
                    }
                }
            }
        }
    }

    @Override
    public boolean stop() {
        return true;
    }
}
